package cn.tedu.drawn.admin.cotroller;

import cn.tedu.drawn.admin.pojo.dto.UserAddDTO;
import cn.tedu.drawn.admin.pojo.dto.UserChangePasswordDTO;
import cn.tedu.drawn.admin.pojo.dto.UserLoginDTO;
import cn.tedu.drawn.admin.pojo.vo.UserListVO;
import cn.tedu.drawn.admin.pojo.vo.UserVO;
import cn.tedu.drawn.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public int login(@RequestBody UserLoginDTO user,
                     HttpSession session, HttpServletResponse response){
        return userService.login(user,session,response);
    }
    @RequestMapping("/currentUser")
    public UserVO currentUser(HttpSession session){
        //把登录成功时保存在会话对象里面的user响应给客户端
        return (UserVO) session.getAttribute("user");
    }
    @RequestMapping("/logout")
    public void logout(HttpSession session){
        //删除会话对象中登录成功时保存的user
        session.removeAttribute("user");
    }

    @RequestMapping("/user/changePassword")
    public void changePassword(UserChangePasswordDTO userChangePasswordDto){
        userService.changePassword(userChangePasswordDto);
    }
    @RequestMapping("/user/select")
    public List<UserListVO> select(){
        return userService.select();
    }
    @RequestMapping("/user/defaultPassword")
    public void defaultPassword(String id){
        userService.defaultPassword(id);
    }

    @RequestMapping("/user/insert")
    public void insert(@RequestBody UserAddDTO userAddDTO){
        userService.insert(userAddDTO);
    }
    @RequestMapping("/user/delete")
    public void delete(int id){
        userService.delete(id);
    }



}
